home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / cprefile.cpp < prev    next >
C/C++ Source or Header  |  2001-03-08  |  3KB  |  154 lines

  1. //    This program is free software; you can redistribute it and/or modify
  2. //    it under the terms of the GNU General Public License as published by
  3. //    the Free Software Foundation; either version 2 of the License, or
  4. //    any later version.
  5. //
  6. //    This program is distributed in the hope that it will be useful,
  7. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. //    GNU General Public License for more details.
  10. //
  11. //    You should have received a copy of the GNU General Public License
  12. //    along with this program; if not, write to the Free Software
  13. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. //
  15.  
  16. #include "cprefile.h"
  17.  
  18. CPreFile::CPreFile()
  19. {
  20.     m_lCacheStartOffset = -1;
  21.     m_lCacheEndOffset = -1;
  22.     m_bOpened = FALSE;
  23.     m_pbtCache = NULL;
  24. }
  25.  
  26. CPreFile::~CPreFile()
  27. {
  28.     Close();
  29. }
  30.  
  31. BOOL CPreFile::Open( CGenFile* pcf, int iCacheSizeKB )
  32. {
  33.     m_lCacheLen = 1024 * iCacheSizeKB;
  34.  
  35.     if ( m_bOpened )
  36.         return FALSE;
  37.  
  38.     m_pcf = pcf;
  39.  
  40.     m_pcf->Seek( 0, SEEK_END );
  41.  
  42.     m_lLength = m_pcf->Tell();
  43.  
  44.     if ( m_lLength < m_lCacheLen )
  45.     {
  46.         m_lCacheLen = m_lLength;
  47.     }
  48.  
  49.     if ( ! ( m_pbtCache = new BYTE [ m_lCacheLen ] ) )
  50.         return FALSE;
  51.  
  52.     m_pcf->Seek( 0, SEEK_SET );
  53.  
  54.     m_lCurrPtr = 0;
  55.  
  56.     m_bOpened = TRUE;
  57.  
  58.     PreRead();
  59.  
  60.     return TRUE;
  61.  
  62. }
  63.  
  64. BOOL CPreFile::PreRead()
  65. {
  66.     m_pcf->Seek( m_lCurrPtr, SEEK_SET );
  67.     m_lCacheStartOffset = m_lCurrPtr;
  68.     m_lCacheEndOffset = m_lCacheStartOffset + m_lCacheLen - 1;
  69.  
  70.     m_pcf->Read( m_pbtCache, m_lCacheLen );
  71.  
  72.     return TRUE;
  73. }
  74.  
  75. BOOL CPreFile::Read( void* pBuff, int iLen, int* piLen )
  76. {
  77.     if ( !m_bOpened )
  78.         return FALSE;
  79.  
  80.     if ( m_lCurrPtr > m_lLength )
  81.         return FALSE;
  82.  
  83.     if ( iLen >= m_lCacheLen )
  84.     {
  85.         m_pcf->Seek( m_lCurrPtr, SEEK_SET );
  86.         if ( !m_pcf->Read( pBuff, iLen, piLen ) )
  87.             return FALSE;
  88.  
  89.         m_lCurrPtr = m_pcf->Tell();
  90.         m_lCacheEndOffset = -1;
  91.         return TRUE;
  92.     }
  93.     
  94.     if ( ( m_lCurrPtr < m_lCacheStartOffset ) || ( m_lCurrPtr > m_lCacheEndOffset ) )
  95.         PreRead();
  96.  
  97.     long lReadEnd = m_lCurrPtr + iLen - 1;
  98.  
  99.     if ( lReadEnd >= m_lCacheEndOffset )
  100.     {
  101.         m_pcf->Seek( m_lCurrPtr, SEEK_SET );
  102.         if ( !m_pcf->Read( pBuff, iLen, piLen ) )
  103.             return FALSE;
  104.  
  105.         m_lCurrPtr = m_pcf->Tell();
  106.         m_lCacheEndOffset = -1;
  107.  
  108.         return TRUE;
  109.     }
  110.  
  111.     memcpy( pBuff, m_pbtCache + m_lCurrPtr - m_lCacheStartOffset, iLen );
  112.     m_lCurrPtr += iLen;
  113.  
  114.     return TRUE;
  115. }
  116.  
  117. BOOL CPreFile::Write( void* pBuff, int iLen, int* piLen )
  118. {
  119.     return FALSE;
  120. }
  121.  
  122. BOOL CPreFile::Seek( long lPos, int iType )
  123. {
  124.  
  125.     switch( iType )
  126.     {
  127.         case SEEK_SET:
  128.             m_lCurrPtr = lPos;
  129.             break;
  130.  
  131.         case SEEK_CUR:
  132.             m_lCurrPtr += lPos;
  133.             break;
  134.  
  135.         case SEEK_END:
  136.             m_lCurrPtr = m_lLength - lPos;
  137.             break;
  138.  
  139.     }
  140.     return TRUE;
  141. }
  142.  
  143. void CPreFile::Close()
  144. {
  145.     m_bOpened = FALSE;
  146.  
  147.     if ( m_pbtCache )
  148.         delete [] m_pbtCache;
  149.  
  150.     m_pbtCache = NULL;
  151.     m_lCurrPtr = 0;
  152. }
  153.  
  154.